package com.eb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eb.annotation.AutoAnnouncement;
import com.eb.annotation.AutoCalculateExp;
import com.eb.context.BaseContext;
import com.eb.domain.Comment;
import com.eb.domain.Innovation;
import com.eb.dto.CommentDto;
import com.eb.enumeration.OperationType;
import com.eb.exception.BaseException;
import com.eb.mapper.ICommentMapper;
import com.eb.mapper.IInnovationMapper;
import com.eb.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CommentServiceImpl extends ServiceImpl<ICommentMapper, Comment> implements CommentService {

    @Autowired
    IInnovationMapper iInnovationMapper;
    /**
     * 新建评论
     * @param commentDto
     */
    @Override
    @AutoAnnouncement(OperationType.COMMENT)
    @AutoCalculateExp(OperationType.COMMENT)
    public Boolean add(CommentDto commentDto) {
        // 判断帖子是否存在
        Innovation innovation = iInnovationMapper.selectById(commentDto.getInnovationId());
        if(innovation == null){
            throw new BaseException("帖子不存在");
        }
        // 1.属性拷贝
        Comment comment = BeanUtil.copyProperties(commentDto, Comment.class);
        // 2.补充属性
        comment.setUserId(BaseContext.getCurrentId());
        comment.setStatus(1);
        comment.setCreateTime(LocalDateTime.now());
        save(comment);
        return true;
    }
}
